package com.huahang.fashionpress.service.impl;

import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.RegistException;
import com.huahang.fashionpress.form.RegistFormUser;
import com.huahang.fashionpress.mapper.UserMapper;
import com.huahang.fashionpress.model.User;
import com.huahang.fashionpress.service.IRegistService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "registServiceImpl")
public class RegistServiceImpl implements IRegistService {
    @Autowired
    private UserMapper userMapper;
    @Override
    //检查用户名是否已经存在
    public boolean userAlreadyExsist(String username) throws RegistException {
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            return false;
        }
        else{
            throw new RegistException(ExceptionEnum.USERNAME_REPEATE);
        }
    }

    //检查邮箱是否已经被使用
    public boolean emailAlreadyExsist(String email)throws RegistException{
        User user = userMapper.selectByEmail(email);
        if (user == null) {
            return false;
        }
        else{
            throw new RegistException(ExceptionEnum.EMAIL_REPEATE);
        }
    }

    //检查电话是否已经被使用
    public boolean phoneAlreadyExsist(String phone)throws RegistException{
        User user = userMapper.selectByPhone(phone);
        if (user == null) {
            return false;
        }
        else{
            throw new RegistException(ExceptionEnum.PHONE_REPEATE);
        }
    }

    //注册新用户
    public boolean insertNewUser(RegistFormUser user){
        if(userMapper.insertNewUser(user.getUserUsername(),user.getUserPassword(),user.getUserEmail(),user.getUserPhone())){
            return true;
        }
        else{
            //throw new RegistException(ExceptionEnum.REGISTFAIL_ERROR);
            return  false;
        }
    }
}
